/* 
 * File:   PacketSIP.cpp
 * Author: Michal Lisicki
 * 
 * Created on January 17, 2012, 1:29 PM
 */

#include <iostream>
#include <stdexcept>
#include "../ApplicationManager.h"

#include "PacketSIP.h"

using namespace PacketHandler;

PacketSIP::PacketSIP() {
    sdp_ = new SDP();
    contents_[0] = NULL;
}

PacketSIP::PacketSIP(const PacketSIP& orig) {
}

PacketSIP::~PacketSIP() {
    delete sdp_;
}

char* PacketSIP::getField(std::string header, unsigned int listPosition) {
    std::list<unsigned int>::iterator it;
    if(headers_.find(header)==headers_.end())
        return NULL;
    try {
        it = headers_[header]->begin();
    } catch (std::out_of_range& oor) {
        return NULL;
    }
    if(it==headers_[header]->end())
        return NULL;
    for(int i=0; i<listPosition;i++) {
        it++;
        if(it==headers_[header]->end())
            return NULL;
    }

    return contents_[*it];
    
//  unsigned int pos;    
//  try {
//       pos = headers_[header];
//  } catch (std::out_of_range& oor) {
//       return NULL;
//  }
//
//  return contents_[pos];
}

void PacketSIP::setField(std::string header, char* content) {
    
}

unsigned int PacketSIP::getFieldPosition(char* header) {

}

void PacketSIP::setFieldAndPosition(unsigned int pos, std::string header, char* content) {
//    char* ch = content;
//    std::cout << "[" << pos << "] header='" << header;
//    std::cout << "' \t\tcontent='";
//    while(*ch!='\n' && *ch!='\r' && *ch!='\0') {
//        std::cout << *ch;
//        ch++;
//    }
//    std::cout << "'";
//    std::cout << std::endl;
//    
    
    if(header!="") {
        if(headers_.find(header)==headers_.end()) {
            std::list<unsigned int>* newList = new std::list<unsigned int>();
            headers_.insert(HMap::value_type(header,newList));
        }
        headers_[header]->push_back(pos);
        contents_[pos] = content;
        contents_[pos+1] = NULL;
    }
    
//    if(header!="") {
//        headers_.insert(HMap::value_type(header,pos));
//        contents_[pos] = content;
//        contents_[pos+1] = NULL;
//    }
}

SDP* PacketSIP::getSDP() {
    return sdp_;
}

void PacketSIP::setSDP(SDP* sdp) {
    sdp_ = sdp;
}

std::string PacketSIP::getMsg() {
    std::string msg = "";
    
    HMap::const_iterator sortedElements[100];
    HMap::const_iterator it;
    std::list<unsigned int>::const_iterator lit;
    for ( it=headers_.begin() ; it != headers_.end(); ++it ) {
        for ( lit=(it->second)->begin() ; lit != (it->second)->end(); ++lit ) {
            sortedElements[(*lit)] = it;
        }
    }

    int i = 0;
    while(contents_[i]!=NULL) {
//        std::cout << sortedElements[i]->first << std::endl;
//        std::string first = sortedElements[i]->first;
        msg += sortedElements[i]->first;
        if(i!=0) msg += ":";
        msg += " ";
        msg += ApplicationManager::getInstance().readLine(contents_[i]);
        msg += "\r\n";
        i++;
    }
    
    msg += "\r\n";
    
    msg += sdp_->getMsg();
    
    return msg;
}